package com.lenovo.agricultureclient.ui.fragment;

import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.request.SetConfigRequest;
import com.lenovo.agricultureclient.ui.dialog.SettingAirDialog;
import com.lenovo.agricultureclient.ui.dialog.SettingCo2Dialog;
import com.lenovo.agricultureclient.ui.dialog.SettingLightDialog;
import com.lenovo.agricultureclient.ui.dialog.SettingPm25Dialog;
import com.lenovo.agricultureclient.ui.dialog.SettingSoilDialog;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
/**
 * 系统设置fragment
 * @author zongbingwu
 *
 */
public class SettingFragment extends AppBaseFragment
{
	public static final String TAG = "SettingFragment";
	
	private LinearLayout mLangLayout;
	private LinearLayout mPm25Layout;
	private LinearLayout mCo2Layout;
	private LinearLayout mLightLayout;
	private LinearLayout mAirLayout;
	private LinearLayout mSoilLayout;
	
	private CheckBox mAutoControlCB;
	//设置所有传感器阀值的请求对象
	private SetConfigRequest mSetConfigRequest;
	private String mUsername = "";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSetConfigRequest = new SetConfigRequest();
		mUsername = mApp.getUsername();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.setting_fragment, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		//初始化界面
		mLangLayout = (LinearLayout)getView().findViewById(R.id.language_layout);
		mLangLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//调用系统语音设置界面
				Intent intent=new Intent(Settings.ACTION_LOCALE_SETTINGS);
				startActivity(intent);
			}
		});
		
		mPm25Layout = (LinearLayout)getView().findViewById(R.id.pm25_setting_layout);
		mPm25Layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//只要当前是admin账号才有设置权限，否则提示用户缺少控制权限
				if(mUsername.equals("admin")){
					//调用设置PM2.5浓度阀值的对话框
					SettingPm25Dialog dialog = new SettingPm25Dialog(getActivity(), mApp);
					dialog.show();
				} else {
					showNoAuthAlertDialog();
				}
			}
		});
		
		mCo2Layout = (LinearLayout)getView().findViewById(R.id.co2_setting_layout);
		mCo2Layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//只要当前是admin账号才有设置权限，否则提示用户缺少控制权限
				if(mUsername.equals("admin")){
					//调用设置CO2浓度阀值的对话框
					SettingCo2Dialog dialog = new SettingCo2Dialog(getActivity(), mApp);
					dialog.show();
				} else {
					showNoAuthAlertDialog();
				}
			}
		});
		
		mLightLayout = (LinearLayout)getView().findViewById(R.id.light_setting_layout);
		mLightLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//只要当前是admin账号才有设置权限，否则提示用户缺少控制权限
				if(mUsername.equals("admin")){
					//调用设置光照强度阀值的对话框
					SettingLightDialog dialog = new SettingLightDialog(getActivity(), mApp);
					dialog.show();
				} else {
					showNoAuthAlertDialog();
				}
			}
		});
		
		mAirLayout = (LinearLayout)getView().findViewById(R.id.air_setting_layout);
		mAirLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//只要当前是admin账号才有设置权限，否则提示用户缺少控制权限
				if(mUsername.equals("admin")){
					//调用设置空气温湿度阀值的对话框
					SettingAirDialog dialog = new SettingAirDialog(getActivity(), mApp);
					dialog.show();
				} else {
					showNoAuthAlertDialog();
				}
			}
		});
		
		mSoilLayout = (LinearLayout)getView().findViewById(R.id.soil_setting_layout);
		mSoilLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//只要当前是admin账号才有设置权限，否则提示用户缺少控制权限
				if(mUsername.equals("admin")){
					//调用设置土壤温湿度阀值的对话框
					SettingSoilDialog dialog = new SettingSoilDialog(getActivity(), mApp);
					dialog.show();
				} else {
					showNoAuthAlertDialog();
				}
			}
		});
		
		mAutoControlCB = (CheckBox)getView().findViewById(R.id.autoControlCheckBox);
		mAutoControlCB.setChecked(mApp.getSensorConfig().controlAuto>0);
		//只要当前是admin账号才有设置权限，否则提示用户缺少控制权限
		if(mUsername.equals("admin"))
		{
			mAutoControlCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
				{
					////向服务器端发送设置自动控制模式请求，0表示手动，1表示自动
					mSetConfigRequest.resetRequest();
					int autoControl = 0;
					if(isChecked){
						autoControl = 1;
					}
					mSetConfigRequest.setAutoControl(autoControl);
					mApp.getSensorConfig().controlAuto = autoControl;
					startRequest(mSetConfigRequest);
				}
			});
		} 
		else 
		{
			mAutoControlCB.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					//控制状态回退
					mAutoControlCB.setChecked(mApp.getSensorConfig().controlAuto>0);
					showNoAuthAlertDialog();
				}
			});
		}
	}
	
	//提示用户缺少控制权限
	private void showNoAuthAlertDialog()
	{
		showAlertDialog(getString(R.string.prompt), getString(R.string.no_setting_authority_exp));
	}
}
